<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="xbsj-labels" content="Earth起步"></meta>
    <title>数据双向绑定</title>
    <!-- 0 引入js文件：XbsjEarth.js和vue.min.js -->
    <script src="../../XbsjEarth/XbsjEarth.js"></script>
    <script src="./scripts/vue.min.js"></script>
    <style>
        html, body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }
    </style>
</head>
<body>
    <div id="vueApp" style="width: 100%; height: 100%; background: grey; position: relative;">
        <earth-comp></earth-comp>
    </div>

    <script>
        // 1 创建Earth的vue组件
        var EarthComp = {
            template: `
                <div style="width: 100%; height: 100%">
                    <div ref="earthContainer" style="width: 100%; height: 100%">
                    </div>
                    <div style="position: absolute; left: 18px; top: 18px; color: white; background: rgba(0, 0, 0, 0.3); padding: 5px; border-radius: 5px;">
                        <span><input type="checkbox" v-model="imageryShow">影像显隐</span><br/>
                        <span>影像透明度：{{ imageryAlpha.toFixed(2) }}</span><br/>
                        <span><input type="range" min="0.0" max="1.0" step="0.01" v-model.number="imageryAlpha"></span><br/>
                    </div>
                </div>
            `,
            data() { 
                return {
                    imageryShow: true,
                    imageryAlpha: 1.0,
                    _earth: undefined, // 注意：Earth和Cesium的相关变量放在vue中，必须使用下划线作为前缀！
                    _bgImagery: undefined,
                };
            },
            // 1.1 资源加载
            mounted() {
                // 1.2.1 创建地球
                var earth = new XE.Earth(this.$refs.earthContainer);

                // 1.2.2 添加默认地球影像
                earth.sceneTree.root = {
                    "children": [
                        {
                            "czmObject": {
                                "name": "默认离线影像",
                                "xbsjType": "Imagery",
                                "xbsjImageryProvider": {
                                    "createTileMapServiceImageryProvider": {
                                        "url": XE.HTML.cesiumDir + 'Assets/Textures/NaturalEarthII',
                                        "fileExtension": 'jpg',
                                    },
                                    "type": "createTileMapServiceImageryProvider"
                                }
                            }
                        },
                    ]
                };
                var bgImagery = earth.sceneTree.root.children[0].czmObject;

                // 1.2.3 双向数据绑定!
                this._showUnbind = XE.MVVM.bind(this, 'imageryShow', bgImagery, 'show');
                this._alphaUnbind = XE.MVVM.bind(this, 'imageryAlpha', bgImagery, 'alpha');

                this._earth = earth;
                this._bgImagery = bgImagery;
            },
            // 1.2 资源销毁
            beforeDestroy() {
                // vue程序销毁时，需要清理相关资源
                this._showUnbind = this._showUnbind && this._showUnbind();
                this._alphaUnbind = this._alphaUnbind && this._alphaUnbind();
                this._bgImagery = this._bgImagery && this._bgImagery.destroy();
                this._earth = this._earth && this._earth.destroy();
            },
        }

        // 2 创建vue程序
        // XE.ready()用来加载Cesium.js等相关资源
        XE.ready().then(() => {
            var app = new Vue({
                el: '#vueApp',
                components: {
                    EarthComp,
                },
            });
        });
    </script>


</body>
</html>